<?php
/**
 * RedCross Application
 *
 * @copyright Copyright (c) 2010, Kristian Stokke Tryggestad, Rune Vikestad, Andreas Flaten Wist, Simen Nicolai Echholt, Ole Henrik Jahren
 */

/**
 * Service for the events
 *
 * @category    RedCross
 * @package     Core_Service
 */
class Core_Service_Event extends RedCross_Service_ServiceAbstract
{

	public function __construct(Core_Gateway_Event $gateway)
	{
		parent::__construct($gateway, 'Core_Model_Event');
	}

	public function create(Core_Model_Event $event)
	{
		$this->checkAcl('create');

		return $this->save($event);
	}

	public function fetchAll($paginate = false)
	{
        $this->checkAcl('fetch');
		return $this->getGateway()->fetchAllEvents($paginate);
	}

    public function fetchAllFuture($paginate = false)
    {
        $this->checkAcl('fetchFuture');
        return $this->getGateway()->fetchAllFuture($paginate);
    }

    public function fetchAllFutureForUser($paginate = false)
    {
        $this->checkAcl('fetchFutureForUser');
        return $this->getGateway()->fetchAllFuture($paginate);
    }

    public function fetchAllPast($paginate = false)
    {
        $this->checkAcl('fetch');
        return $this->getGateway()->fetchAllPast($paginate);
    }
	
	public function fetchAllWithOnlyIdAndName()
	{
		return $this->getGateway()->fetchAllWithOnlyIdAndName();
	}
	
	public function deleteEventAndChildShifts($eventId)
	{
		return $this->getGateway()->delete($eventId);
	}

    public function fetchAllByUser($userId, $paginate = false)
    {
        $this->checkAcl('fetchFutureForUser');
        return $this->getGateway()->fetchAllByUser($userId, $paginate);
    }

    public function fetchEventsThisWeek($today, $endOfWeek) {
       return $this->getGateway()->fetchEventsThisWeek($today, $endOfWeek); 
    }

	static public function check_shift_coverage($view, $event, $shifts)
	{
		$max_end_time = new RedCross_Date($event->from_date, RedCross_Date::DB_FMT);

		if (!isset($view->flash))
			$view->flash = array();

		foreach ($shifts as $shift)
		{
			$start_time = new RedCross_Date($shift['from_date'], RedCross_Date::DB_FMT);
			$end_time = new RedCross_Date($shift['to_date'], RedCross_Date::DB_FMT);
			if ($start_time->getTimestamp() > $max_end_time->getTimestamp())
			{
				$view->flash[] = $view->flashMessage()->renderMessage(
					'Tidrommet ' .
					$max_end_time->getDateTimeStr() .
					' - ' . $start_time->getDateTimeStr() .
					' er ikke dekket av noen vakt',
					RedCross_View_Helper_FlashMessage::WARNING);
			}

			if ($max_end_time->getTimestamp() < $end_time->getTimestamp())
			{
				$max_end_time = $end_time;
			}
		}
		$event_end_time = new RedCross_Date($event->to_date, RedCross_Date::DB_FMT);
		if ($event_end_time->getTimestamp() > $max_end_time->getTimestamp())
		{
			$view->flash[] = $view->flashMessage()->renderMessage(
				'Tidrommet ' .
				$max_end_time->getDateTimeStr() .
				' - ' . $event_end_time->getDateTimeStr() .
				' er ikke dekket av noen vakt',
				RedCross_View_Helper_FlashMessage::WARNING);
		}
	}
    
}
